from django.db import models


class Category(models.Model):
    """分类模型，支持三级分类结构"""
    name = models.CharField(max_length=100, verbose_name="分类名称")
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='children',
        verbose_name="父级分类"
    )
    level = models.PositiveSmallIntegerField(verbose_name="层级（1-3）")
    is_active = models.BooleanField(default=True, verbose_name="是否启用")
    sort_order = models.PositiveIntegerField(default=0, verbose_name="排序权重")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = "分类"
        verbose_name_plural = "分类"
        ordering = ['level', 'sort_order', 'name']

    def __str__(self):
        """显示完整分类路径"""
        if self.parent:
            return f"{self.parent} → {self.name}"
        return self.name
